import Vue from 'vue';
import Router from 'vue-router';
import store from '@/store/store';
// import testFile from "../pages/testFile";
const index = resolve => require(['@/pages/index/index'], resolve);
const entrance = resolve => require(['@/pages/entrance/entrance'], resolve);
const login = resolve => require(['@/pages/login/login'], resolve);
const register = resolve => require(['@/pages/register/register'], resolve);
const edit_pws = resolve => require(['@/pages/edit_pws/edit_pws'], resolve);
const set_transaction_pws = resolve => require(['@/pages/set_transaction_pws/set_transaction_pws'], resolve);
const edit_transaction_pws = resolve => require(['@/pages/edit_transaction_pws/edit_transaction_pws'], resolve);
const my_power = resolve => require(['@/pages/my_power/my_power'], resolve);
const my_account = resolve => require(['@/pages/my_account/my_account'], resolve);
const recharge = resolve => require(['@/pages/recharge/recharge'], resolve);
const recharge_public = resolve => require(['@/pages/recharge_public/recharge_public'], resolve);
const power_list = resolve => require(['@/pages/power_list/power_list'], resolve);
const place_order = resolve => require(['@/pages/place_order/place_order'], resolve);
// const place_order2 = resolve => require(['@/pages/place_order2/place_order2'], resolve);
const chose_foreign = resolve => require(['@/pages/chose_foreign/chose_foreign'], resolve);
const china_verification = resolve => require(['@/pages/china_verification/china_verification'], resolve);
const upload_idcard = resolve => require(['@/pages/upload_idcard/upload_idcard'], resolve);
const foreign_verification = resolve => require(['@/pages/foreign_verification/foreign_verification'], resolve);
const uploadidcard_foreign = resolve => require(['@/pages/uploadidcard_foreign/uploadidcard_foreign'], resolve);
const setting = resolve => require(['@/pages/setting/setting'], resolve);
const transaction_password = resolve => require(['@/pages/transaction_password/transaction_password'], resolve);
const add_bankcard = resolve => require(['@/pages/add_bankcard/add_bankcard'], resolve);
const cash_withdraw = resolve => require(['@/pages/cash_withdraw/cash_withdraw'], resolve);
const my_banks_coinsaddress = resolve => require(['@/pages/my_banks_coinsaddress/my_banks_coinsaddress'], resolve);
const announcement_list = resolve => require(['@/pages/announcement_list/announcement_list'], resolve);
const announcement_detail = resolve => require(['@/pages/announcement_detail/announcement_detail'], resolve);
const out_coins = resolve => require(['@/pages/out_coins/out_coins'], resolve);
const out_select_coins = resolve => require(['@/pages/out_select_coins/out_select_coins'], resolve);
const audit_in = resolve => require(['@/pages/audit_in/audit_in'], resolve);
const audit_pass = resolve => require(['@/pages/audit_pass/audit_pass'], resolve);
const audit_notpass = resolve => require(['@/pages/audit_notpass/audit_notpass'], resolve);
const forget_pws = resolve => require(['@/pages/forget_pws/forget_pws'], resolve);
const no_electric_power_introduce = resolve => require(['@/pages/power_introduce/power_introduce'], resolve);
const no_electric_power_pay = resolve => require(['@/pages/no_electric_power_pay/no_electric_power_pay'], resolve);
const chose_coupon = resolve => require(['@/pages/chose_coupon/chose_coupon'], resolve);
const is_electric_power_pay = resolve => require(['@/pages/need_electric_power_pay/need_electric_power_pay'], resolve);
const not_return_power_detail = resolve => require(['@/pages/not_return_power_detail/not_return_power_detail'], resolve);
const no_electric_add_electric = resolve => require(['@/pages/no_electric_add_electric/no_electric_add_electric'], resolve);
const no_electric_recharge_detail = resolve => require(['@/pages/no_electric_recharge_detail/no_electric_recharge_detail'], resolve);
const increment_detail = resolve => require(['@/pages/increment_detail/increment_detail'], resolve);
const power_product = resolve => require(['@/pages/power_product/power_product'], resolve);
const power_product_detail = resolve => require(['@/pages/power_product_detail/power_product_detail'], resolve);
const coupon = resolve => require(['@/pages/coupon/coupon'], resolve);
const ordinaryDetail = resolve => require(['@/pages/ordinaryDetail/ordinaryDetail'], resolve);
const doubleDetail = resolve => require(['@/pages/doubleDetail/doubleDetail'], resolve);
const about_us = resolve => require(['@/pages/about_us/about_us'], resolve);

/*------税酬页面  无需登录  app内部打开------*/
// const taxReward_testFile = resolve => require(['@/pages/testFile.vue'], resolve);
const taxReward_list = resolve => require(['@/pages/taxReward/list'], resolve);
const taxReward_step1 = resolve => require(['@/pages/taxReward/step_1'], resolve);
const taxReward_step2 = resolve => require(['@/pages/taxReward/step_2'], resolve);
const taxReward_step3 = resolve => require(['@/pages/taxReward/step_3'], resolve);
const taxReward_step4 = resolve => require(['@/pages/taxReward/step_4'], resolve);
const taxReward_stepCancel = resolve => require(['@/pages/taxReward/step_cancel'], resolve);
/*--------------------*/
Vue.use(Router);

let wapRouterArr = [
  {
    path: '/', redirect: '/entrance'
  },
  // {
  //   path: '/index', component: index,
  //   meta:{index:0,title:'首页'}
  // },
  {
    path: '/login', component: login,
    meta:{index:1,title:'登录'}
  },
  {
    path: '/register', component: register,
    meta:{index:2,title:'注册'}
  },
  {
    path: '/entrance', component: entrance,
    meta:{index:3,title:'中心'}
  },
  {
    path: '/coupon', component: coupon,
    meta:{index:4,title:'优惠券'}
  },
  {
    path: '/about_us', component: about_us,
    meta:{index:5,title:'关于我们'}
  },
  {
    path: '/power_product', component: power_product,
    meta:{index:4,title:'算力产出'}
  },
  {
    path: '/power_product_detail', component: power_product_detail,
    meta:{index:5,title:'算力产出详情'}
  },
  {
    path: '/power_introduce', component: no_electric_power_introduce,
    meta:{index:4,title:'订单介绍'}
  },
  {
    path: '/no_electric_power_pay', component: no_electric_power_pay,
    meta:{index:5,title:'订单支付'}
  },
  {
    path: '/chose_coupon', component: chose_coupon,
    meta:{index:6,title:'选择优惠券'}
  },
  {
    path: '/need_electric_power_pay', component: is_electric_power_pay,
    meta:{index:5,title:'订单支付'}
  },
  {
    path: '/announcement_list', component: announcement_list,
    meta:{index:4,title:'公告列表'}
  },
  {
    path: '/announcement_detail', component: announcement_detail,
    meta:{index:5,title:'公告详情'}
  },
  {
    path: '/setting', component: setting,
    meta:{index:7,title:'设置'}
  },
  {
    path: '/transaction_password', component: transaction_password,
    meta:{index:8,title:'交易密码'}
  },
  {
    path: '/edit_pws', component: edit_pws,
    meta:{index:8,title:'修改登录密码'}
  },
  {
    path: '/set_transaction_pws', component: set_transaction_pws,
    meta:{index:9,title:'设置交易密码'}
  },
  {
    path: '/forget_pws', component: forget_pws,
    meta:{index:8,title:'忘记登录密码'}
  },
  {
    path: '/edit_transaction_pws', component: edit_transaction_pws,
    meta:{index:9,title:'修改交易密码'}
  },

  {
    path: '/not_return_power_detail', component: not_return_power_detail,
    meta:{index:7,title:'合约包详情'}
  },
  {
    path: '/increment_detail', component: increment_detail,
    meta:{index:8,title:'增值服务详情'}
  },
  {
    path: '/no_electric_add_electric', component: no_electric_add_electric,
    meta:{index:8,title:'充值电费'}
  },
  {
    path: '/no_electric_recharge_detail', component: no_electric_recharge_detail,
    meta:{index:9,title:'电费充值详情'}
  },

  {
    path: '/my_power', component: my_power,
    meta:{index:6,title:'我的算力'}
  },
  {
    path: '/cash_withdraw', component: cash_withdraw,
    meta:{index:6,title:'提现'}
  },
  {
    path: '/my_account', component: my_account,
    meta:{index:6,title:'我的账户'}
  },
  {
    path: '/out_select_coins', component: out_select_coins,
    meta:{index:7,title:'提币-选择币种'}
  },
  {
    path: '/out_coins', component: out_coins,
    meta:{index:8,title:'提币'}
  },
  {
    path: '/my_banks_coinsaddress', component: my_banks_coinsaddress,
    meta:{index:9,title:'我的银行卡/提币地址管理'}  ///提币地址
  },
  {
    path: '/add_bankcard', component: add_bankcard,
    meta:{index:10,title:'添加银行卡'}
  },
  {
    path: '/power_list', component: power_list,
    meta:{index:6,title:'算力列表'}
  },
  {
    path: '/ordinaryDetail', component: ordinaryDetail,
    meta:{index:6,title:'合约包详情'}
  },
  {
    path: '/doubleDetail', component: doubleDetail,
    meta:{index:6,title:'合约包详情'}
  },
  {
    path: '/place_order', component: place_order,
    meta:{index:7,title:'支付订单'}
  },
  // {
  //   path: '/place_order2', component: place_order2,
  //   meta:{index:7,title:'支付订单'}
  // },
  {
    path: '/recharge', component: recharge,
    meta: {index: 8,title:'充值'}
  },
  {
    path: '/recharge_public', component: recharge_public,
    meta:{index:9,title:'对公转账'}
  },
  {
    path: '/china_verification', component: china_verification,
    meta:{index:10,title:'证件信息'}
  },
  {
    path: '/foreign_verification', component: foreign_verification,
    meta:{index:11,title:'证件信息(海外)'}
  },
  // {
  //   path: '/recharge_confirm', component: recharge_confirm,
  //   meta:{index:10,title:'确认转账'}
  // },
  {
    path: '/audit_in', component: audit_in,
    meta:{index:8,title:'审核中'}
  },
  {
    path: '/audit_pass', component: audit_pass,
    meta:{index:8,title:'审核通过'}
  },
  {
    path: '/audit_notpass', component: audit_notpass,
    meta:{index:8,title:'审核失败'}
  },
  {
    path: '/chose_foreign', component: chose_foreign,
    meta:{index:9,title:'选择国籍'}
  },
  {
    path: '/uploadidcard_foreign', component: uploadidcard_foreign,
    meta:{index:11,title:'上传证件(海外用户)'}
  },
  {
    path: '/upload_idcard', component: upload_idcard,
    meta:{index:11,title:'上传证件(国内用户)'}
  },
];//wap网页端的默认页面

let appWapTaxRouter = [
  {
    path: '/taxReward_list', component: taxReward_list,
    meta:{title:'记录'}
  },
  {
    path: '/taxReward_step1', component: taxReward_step1,
    meta:{title:'查看任务'}
  },
  {
    path: '/taxReward_step2', component: taxReward_step2,
    meta:{title:'查看任务'}
  },
  {
    path: '/taxReward_step3', component: taxReward_step3,
    meta:{title:'查看任务'}
  },
  {
    path: '/taxReward_step4', component: taxReward_step4,
    meta:{title:'查看任务'}
  },
  {
    path: '/taxReward_stepCancel', component: taxReward_stepCancel,
    meta:{title:'查看任务'}
  },
];//app远程打开的wap税酬页面  只要是app端打开的，都无需验证登录 ！

const router = new Router({
  mode : "hash",//路由模式
  routes : [ wapRouterArr , appWapTaxRouter ].flat(10),
  // routes : [ ...wapRouterArr , ...appWapTaxRouter ]
});

export default router;

if (localStorage.token) {
  store.commit('token', localStorage.token)
}
if (localStorage.header) {
  store.commit('header', localStorage.header)
}
if (localStorage.uid) {
  store.commit('uid' , localStorage.uid)
}
if (localStorage.mobile) {
  store.commit('mobile' , localStorage.mobile)
}

const acceptViews = [
  '/login','/index','/register', '/forget_pws' , '/entrance' , '/upload_idcard' , '/about_us',
  '/ordinaryDetail', '/doubleDetail','/power_introduce',
];//未登录/后端信息验证不正确，允许访问的页面，其他页面不允许访问

const needStoreChoseCoupons = [
  '/need_electric_power_pay' ,  '/no_electric_power_pay' , '/chose_coupon'
];//选择的优惠券，需要保存store的页面，其他页面删除store优惠券

router.beforeEach( function( to , from , next ) {
  if (to.meta.title) {
    document.title = '人人矿场 - ' + to.meta.title;
  }
  if(needStoreChoseCoupons.indexOf(to.path) === -1 ){
    store.commit('chose_coupons' , []); //清除优惠券选择
    store.commit('rent_num' , ''); //清除租用数量
    store.commit('buy_math_power' , false); //清除购买算力宝
    store.commit('payType' , ''); //清除支付方式
  }
  let queryHaveInviterCode = { inviter_code : to.query.inviter_code || store.state.inviter_code  };//inverter_code每个页面都带上参数，以防随时使用
  let toPath = to.path;//前进路由
  if( to.matched.length === 0 ){
    //进入不存在路由，默认回上一页，上一页不存在则回个人主页
    from.path ?
      next({path : from.path , query : queryHaveInviterCode , replace : true}) :
      next ({path:'/entrance', query : queryHaveInviterCode , replace : true});
  } else {
    if ( !store.state.uid ) {
      let isAppWapPage =  to.path.indexOf('/taxReward_') != -1; //判断是app打开的远程wap页面
      if(isAppWapPage){
        next({to : toPath , query :queryHaveInviterCode}); //app内部打开的wap远程页面。直接进入，不需要验证登录
        return;
      }
      if( acceptViews.indexOf(to.path) === -1 ){
        require('../common/js/toast').toast('请先登录');
        next ({path : '/login' , query :{ to : to.fullPath , inviter_code : to.query.inviter_code || store.state.inviter_code  }});
        //验证信息不正确，默认进入登录页面
      } else {
        next({to : toPath , query :queryHaveInviterCode});
      }
    } else {
      next({to : toPath , query :queryHaveInviterCode});
    }
  }
});

router.afterEach( function() {
  window.scrollTo(0, 0);
  let app = document.getElementById('app');
  app.scroll(0,0);
  app = null;
});
